Skip to content

fix: settle cancelled evaluate_script calls - #3575

Open
JasonOA888 wants to merge 5 commits into
ChromeDevTools:mainfrom
JasonOA888:fix/evaluate-script-cancellation
Open

JasonOA888 wants to merge 5 commits into
ChromeDevTools:mainfrom
JasonOA888:fix/evaluate-script-cancellation

Conversation

@JasonOA888

@JasonOA888 JasonOA888 commented Nov 21, 2026 •

Copy link
Copy Markdown

Refs #2934

#3079 made cancellation stop waiting, but work that is already running still holds its task queue until it settles. evaluate_script runs on the global queue because it is not page-scoped. So if the client cancels a call whose function never settles, every later tool call (list_pages, take_snapshot on another page, ...) waits behind it. This is Case B in the latest re-test. Today the only things that end it are the protocol timeout or the page going away.

Change

  • ToolHandler passes the request's AbortSignal to tool handlers as request.signal. Request gets an optional signal. This is the same field, in the same place, that fix: forward MCP request cancellation to WebMCP tool calls #3411 adds for WebMCP, so whichever PR lands second has a one-line conflict.
  • performEvaluation() races the evaluation with the signal in Node, using the existing raceWithSignal(). On cancellation the handler rejects with the signal's AbortError and the queue is released. The race sits inside the action passed to waitForEventsAfterAction(), so the wait helper still cleans up its listeners. Pages and service workers share this path.
  • A request that is already cancelled when performEvaluation() starts throws before anything is sent to the page, so the user's function never runs.
  • Both formats make two protocol calls: evaluateHandle() resolves the function or the script result, then evaluate() runs or serializes it. The signal is checked again between them, so a request cancelled during the first call does not send the second.

Behavior changes

  • A cancelled evaluate_script now releases the task queue right away instead of holding it until the evaluation settles. This holds for both formats and for synchronous loops.
  • An evaluate_script call whose request is already cancelled no longer runs the function.
  • A call cancelled while its first protocol call is in flight no longer sends the second one, so a function-format call never starts the user's function.
  • Without a signal, or when it never aborts, evaluation is unchanged: no extra protocol calls, same result, error normalization and filePath output.

Not covered

  • The abandoned evaluation keeps running in the page or worker until it settles, the protocol timeout hits or the target goes away. Stopping it needs Runtime.terminateExecution, which also stops unrelated page scripts, so I left it out. A synchronous loop therefore still blocks later tools on the same page, but no longer the global queue.
  • execute_webmcp_tool / execute_3p_developer_tool. That is fix: forward MCP request cancellation to WebMCP tool calls #3411.

Testing

  • tests/tools/script.test.ts: five new mock-based tests, each fails on main and passes with this change:
    • a pending page evaluation (evaluate never settles): abort after it starts, expect AbortError;
    • the same pending case with serviceWorkerId;
    • an already cancelled request: expect AbortError and no evaluateHandle() call. This test also fails if only the early throwIfAborted() is removed;
    • one test per format that aborts during evaluateHandle(), asserts its exact source, and checks that evaluate() is never called.
  • tests/ToolHandler.test.ts: new test that the request signal reaches page, browser and browserless handlers. I updated the existing exact-argument assertions for the new field.
  • npm run format and npm run build pass. With npm run test, every failure is in the CLI e2e suites (tests/e2e/chrome-devtools-*.test.ts), and those suites fail the same way on an unmodified main in my environment.

@OrKoN
OrKoN self-requested a review November 21, 2026 17:37

@OrKoN OrKoN left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please check the following:

  1. src/tools/script.ts, performEvaluation(): Replace rejectOnAbort() and the page-side Promise.race with raceWithSignal(evaluatable.evaluate(...), signal) from src/utils/raceWithSignal.ts, or state in the PR description why the page-side promise is necessary. The task queue is released when the handler settles, so a Node-side race gives the same result with no added protocol round trip, also covers format: "script" and a synchronous loop, and does not depend on a Promise.race that the page can replace.

  2. src/tools/script.ts, rejectOnAbort(): If the page-side promise stays, do not start the main evaluatable.evaluate() when the signal aborts between signal.throwIfAborted() and that call. In that interval onAbort rejects aborted before a handler exists, so the page gets an unhandledrejection event and the user function still starts after the cancellation.

  3. src/tools/script.ts, rejectOnAbort(): If the page-side promise stays, return before evaluatable.evaluateHandle() when signal is undefined. The current code creates a page object and a promise that no caller can reject.

  4. tests/tools/script.test.ts: Add a test for a signal that is already aborted when performEvaluation() starts, and a test for cancellation with serviceWorkerId. The new test calls only the page path with an abort during the evaluation, so the signal.throwIfAborted() branch and the service worker call site have no test.

@OrKoN
OrKoN self-requested a review November 23, 2026 13:52
@JasonOA888

Copy link
Copy Markdown
Author

1. Node-side race instead of the page-side promise

Agreed, and changed. performEvaluation() now races the evaluation with raceWithSignal() and the page-side promise is gone. As you say, this also covers format: "script" and synchronous loops, adds no round trip and does not depend on page globals. I kept the race inside the action passed to waitForEventsAfterAction() rather than around the whole handler, so the wait helper still removes its listeners when the action rejects. The call now rejects with the signal's AbortError instead of a custom message.

2. Abort between throwIfAborted() and the main evaluate()

rejectOnAbort() is removed, so the unhandledrejection case no longer exists. The other half of the point still applied to the new code, because raceWithSignal() rejects an already aborted signal but the evaluation would still have been sent. performEvaluation() now calls signal?.throwIfAborted() in the same synchronous step that starts the evaluation, so a request cancelled before performEvaluation() runs never reaches the page. A cancellation that arrives later rejects the call at once; the function may still start in the page, which is the same outcome as cancelling a moment after it started.

3. Page object created when there is no signal

No longer applies: with no signal, performEvaluation() awaits the evaluation directly and creates nothing extra in the page.

4. Tests

Added both: does not start an evaluation for an already cancelled request (expects AbortError and checks the function did not run) and rejects a pending service worker evaluation when the request is cancelled. Both fail on main and pass with this change. I also updated the PR description.

@OrKoN
OrKoN enabled auto-merge November 23, 2026 15:01
@OrKoN
OrKoN added this pull request to the merge queue Nov 23, 2026
@github-merge-queue
github-merge-queue Bot removed this pull request from the merge queue due to failed status checks Nov 23, 2026
evaluate_script is not page-scoped, so it runs on the global task queue.
When the client cancels a call whose function never settles, ToolHandler
stops waiting but the evaluation keeps the queue, and every later tool
call waits behind it.

Tool handlers now receive the request signal. evaluate_script races the
user function against a page-side promise that rejects when the signal
aborts, so the protocol call returns and the queue is released.

Refs ChromeDevTools#2934
Settle the handler as soon as the MCP request is cancelled instead of
rejecting a page-side promise. This also covers format: "script" and
synchronous loops, costs no extra protocol round trip and does not
depend on page globals. An already cancelled request no longer starts
the evaluation.
@OrKoN
OrKoN force-pushed the fix/evaluate-script-cancellation branch from 0dba818 to b376c7b Compare November 23, 2026 15:20

@OrKoN OrKoN left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like tests are failing

The slim mode tests from ChromeDevTools#3565 assert the exact handler request, which
now includes the MCP request signal.
@JasonOA888

Copy link
Copy Markdown
Author

Thanks. The failures were the two slim mode tests in tests/ToolHandler.test.ts (creates a SlimMcpResponse for slim tools and creates an McpResponse that is not a SlimMcpResponse for other tools). They came in with #3565 after this PR was opened, and they assert the exact handler request, which now also carries signal. 2e9403a updates both assertions to expect the signal, as the other ToolHandler tests in this PR already do. No production code changed. npm run format and npm run build pass, and the unit tests pass locally.

@OrKoN
OrKoN requested review from Lightning00Blade and OrKoN and removed request for OrKoN November 25, 2026 07:10
Comment thread src/tools/script.ts Resolved
Both formats resolve a handle first and send a second Runtime call to
run or serialize it. Check the signal in between so a request cancelled
during the first call does not start the second one.

@OrKoN OrKoN left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please check the following:

  1. In tests/tools/script.test.ts, replace the real-browser test does not start an evaluation for an already cancelled request with a mock test that asserts sinon.assert.notCalled(page.pptrPage.evaluateHandle). The current test also passes when the first signal?.throwIfAborted() in performEvaluation() is removed, because the check after evaluateHandle() also stops the function.

  2. In tests/tools/script.test.ts, replace the two real-browser tests rejects a pending evaluation when the request is cancelled and rejects a pending service worker evaluation when the request is cancelled with mock tests that stub evaluate with a promise that never settles (createHandlerMocks(), createMockWebWorker(), createMockMcpWorker()). AGENTS.md permits real-browser tests only for behavior that mocks cannot observe, and the service worker test installs an extension and polls evaluate_script up to 50 times.

  3. In the two skips the second ${format} CDP call on abort tests, replace sinon.assert.calledOnce(page.pptrPage.evaluateHandle) with sinon.assert.calledOnceWithExactly(...) and the expected source string. The AGENTS.md checklist requires exact-argument assertions when the handler derives the argument from its input.

Replace the real-browser cancellation tests with mock tests. The
already-cancelled test now asserts that no CDP call is sent, which the
later abort check alone does not satisfy, and the second-call tests
assert the exact source passed to evaluateHandle().
@JasonOA888

Copy link
Copy Markdown
Author

Thanks, all three addressed in 094c291. Only tests changed.

  1. does not start an evaluation for an already cancelled request is now a mock test that asserts sinon.assert.notCalled(page.pptrPage.evaluateHandle). I checked that it fails when the first signal?.throwIfAborted() in performEvaluation() is removed.
  2. Both pending tests are now mock tests. evaluate is stubbed with a promise that never settles, the test waits until it has been called, then aborts and expects AbortError. The service worker variant uses createMockWebWorker() and createMockMcpWorker() through context.getWorkerById(), so the extension install and the polling loop are gone. Each has a 5s timeout because on main the handler never settles.
  3. The skips the second ${format} CDP call on abort tests now use sinon.assert.calledOnceWithExactly with the exact source: (\n() => 1\n) for function and 1 + 1 for script.

All five tests fail on main and pass with the change. npm run format, npm run build and the script and ToolHandler tests pass locally. I also updated the Testing section of the description.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants